package com.erb.controller.llb;

import com.erb.constants.AdminIntegralGoodsConstants;
import com.erb.constants.AdminRedpacketConstants;
import com.erb.constants.IntegralGoodsRecordConstants;
import com.erb.entity.*;
import com.erb.service.llb.IOtherService;
import com.erb.service.zwj.IIntegralGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 积分商城
 */
@RestController
@RequestMapping("/base/mallGoods")
public class MallController {

	@Autowired
	IIntegralGoodsService integralGoodsService;
	@Autowired
	com.erb.service.ljy.integralService integralService;
	@Autowired
	IOtherService otherService;

	/**
	 * 查询商品列表
	 *
	 * @return
	 */
	@GetMapping("/goodsList")
	public RespBean goodsList() {
		List<IntegralGoods> allGoodsForList = integralGoodsService.findGoodsForListOfPutaway();
		return RespBean.ok("查询成功", allGoodsForList);
	}

	/**
	 * 查询详情
	 *
	 * @param gid
	 * @return
	 */
	@GetMapping("/detail/{gid}")
	public RespBean detail(@PathVariable int gid) {
		return RespBean.ok("查询成功", integralGoodsService.findGoodsById(gid));
	}

	/**
	 * 更新用户积分
	 *
	 * @param user
	 * @param integral
	 * @return
	 */
	@PutMapping("/updateIntegral")
	public RespBean updateIntegral(@SessionAttribute("user") User user, int integral) {
		integralService.addIntegralByUser(user.getId(), integral);
		return RespBean.ok("success");
	}

	@PostMapping("/addRecord")
	public RespBean addRecord(@RequestBody IntegralGoodsRecord record, @SessionAttribute("user") User user) {
		IntegralGoods goods = integralGoodsService.findGoodsById(record.getGid());
		record.setUid(user.getId());
		record.setExchangeTime(new Date());
		//如果是红包
		if (goods.getType() == AdminIntegralGoodsConstants.INTEGRAL_GOODS_TYPE_REDPACKET) {
			record.setState(IntegralGoodsRecordConstants.RECORD_STATE_FINISHED);
			for (Integer i = 0; i < record.getCount(); i++) {
				Redpacketrecord redpacketrecord = new Redpacketrecord();
				redpacketrecord.setUid(user.getId());
				redpacketrecord.setName(goods.getName());
				redpacketrecord.setDescribe(goods.getName());
				redpacketrecord.setRtype(AdminRedpacketConstants.REDPACKET_RTYPE_NO);
				redpacketrecord.setAmount(goods.getMoney());
				redpacketrecord.setExchangeCode("无");
				redpacketrecord.setThreshold(0);
				redpacketrecord.setPtype(0);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, 7);
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				redpacketrecord.setDeadline(format.format(calendar.getTime()));
				redpacketrecord.setState(1);
				otherService.addRedPacket(redpacketrecord);
			}
		} else {
			record.setState(IntegralGoodsRecordConstants.RECORD_STATE_WAITTING);
		}
		if (otherService.addRecord(record) > 0) {
			integralService.addIntegralByUser(user.getId(), goods.getPrice() * record.getCount() * -1);
			integralGoodsService.decrGoodsStock(goods.getId(), record.getCount() * -1);
			return RespBean.ok("添加纪录成功");
		} else {
			return RespBean.error("添加失败");
		}
	}

	@GetMapping("/todayExchange")
	public RespBean todayExchange(@SessionAttribute("user") User user, int gid) {
		Integer i = otherService.todayExchange(user.getId(), gid);
		return RespBean.ok("查询成功", i == null ? 0 : i);
	}

	@GetMapping("/userIntegral")
	public RespBean userIntegral(@SessionAttribute("user") User user) {
		Integer i = otherService.userIntegral(user.getId());
		return RespBean.ok("查询成功", i == null ? 0 : i);
	}
}
